package com.mk.common.util;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

public class RSADemo {
	// 角色信息
	private static String jsonStr = "[\r\n" + 
					"        {\"roleId\":\"sqzg.CKJS\", \"roleName\":\"查看角色\"},\r\n" + 
					"        {\"roleId\":\"sqzg.gpys\", \"roleName\":\"GP医生\"},\r\n" + 
					"        {\"roleId\":\"sqzg.kszxzg\", \"roleName\":\"科室-中心主管\"},\r\n" + 
					"        {\"roleId\":\"sqzg.study\", \"roleName\":\"学习与培训\"},\r\n" + 
					"        {\"roleId\":\"sqzg.system\", \"roleName\":\"系统管理员\"},\r\n" + 
					"        {\"roleId\":\"sqzg.ystbr\", \"roleName\":\"预算填报员\"},\r\n" + 
					"        {\"roleId\":\"sqzg.ywgly\", \"roleName\":\"业务管理员\"},\r\n" + 
					"        {\"roleId\":\"sqzg.yyld\", \"roleName\":\"医院领导\"}\r\n" + 
					"    ]";
	// 公钥
	private static String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCP26pF0S3C/u54TIdYFxQk2xmIiJO4qOXyf72KFb32ZSIyUE0yNLysLcjUYJPm52VQjrq/Zmd2ap7z1GyfjUnNNncKsN1ed11pgLOIkIt8+wUTSJ49Fors0IHOILC/EAXL4c+utxwLTJ6/8Iq6tahHo5Fs7RAjkJHxlP2A3KU87QIDAQAB";
	
	@Before
	public void setUp() throws Exception
	{
	}
	
	
	@Test
	public void test1() {
		String as = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCP26pF0S3C/u54TIdYFxQk2xmIiJO4qOXyf72KFb32ZSIyUE0yNLysLcjUYJPm52VQjrq/Zmd2ap7z1GyfjUnNNncKsN1ed11pgLOIkIt8+wUTSJ49Fors0IHOILC/EAXL4c+utxwLTJ6/8Iq6tahHo5Fs7RAjkJHxlP2A3KU87QIDAQAB";
		byte[] data = as.getBytes();
		
		System.out.println(data.length); // 216
		System.out.println(data);
	}
	
	@Test
	public void test2() {
		JSONArray jsonArray = JSON.parseArray(jsonStr);
		Map<String,Object> param = new HashMap<>(1);
		param.put("roleList", jsonArray);
		param.put("appId", "SSOA202112171523491084");
		
		Map<String,Object> initParam = new HashMap<>(2);
		initParam.put("initParams", param);
		initParam.put("interfaceCode", "SSOMH003");
		System.out.println( JSON.toJSONString(initParam) );
	}
	
	@Test
	public void test2_() {
		JSONArray jsonArray = JSON.parseArray(jsonStr);
		Map<String,Object> param = new HashMap<>();
		param.put("roleList", jsonArray);
		param.put("appId", "SSOA202112171523491084");
		
		param.put("initParams", param);
		param.remove("appId");
		param.remove("roleList");
		param.put("interfaceCode", "SSOMH003");
		System.out.println( JSON.toJSONString(param) );
	}
	
	
	@Test
	public void test3() throws Exception {
		JSONArray jsonArray = JSON.parseArray(jsonStr);
		String ecrpt = RSAUtils.encryptByPublicKey(jsonArray.toString(), pubKey);
		System.out.println(ecrpt);
	}
	
	@Test
	public void test4() throws Exception {
		// 生成新的公私钥
		Map<String, Object> genKeyPair = RSAUtils.resetGenKeyPair();

		// 获取公钥
		String publicKey = RSAUtils.getPublicKey(genKeyPair);
		System.out.println("------- 公钥 -------");
		System.out.println(publicKey);

		// 获取私钥
		String privateKey = RSAUtils.getPrivateKey(genKeyPair);
		System.out.println("------- 私钥 -------");
		System.out.println(privateKey);

//				String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyJn4YYj+Ng/xjOPvGrt3MVLZ+xk3yjqBUu5U5tO1xC1Q/Wae8gWpIC3ipS9UVMCNUOKm2GHdQFI1EXaAu0bRQhBb4aE5IdXrT9Xoo4Zeuv/ut9UvKEpjBB7Geiy6OvAoA0ROBRLA9/j9W8jZraWiirXRAxI7ZyqgZSr5lHgJWdwIDAQAB";
//				String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALImfhhiP42D/GM4+8au3cxUtn7GTfKOoFS7lTm07XELVD9Zp7yBakgLeKlL1RUwI1Q4qbYYd1AUjURdoC7RtFCEFvhoTkh1etP1eijhl66/+631S8oSmMEHsZ6LLo68CgDRE4FEsD3+P1byNmtpaKKtdEDEjtnKqBlKvmUeAlZ3AgMBAAECgYB0/En5gSrypyVpktXjFpmXwFlGzroI+hfXDIdlqaXygdoE777yTpmYTdAifCWlEENi3wKzDUXsVFKf/ktd819QF4gHCby8Hlk39Gj1itUJ90V7XZVWgJJYMaudGg4rKa3w+VYhw2fut5EXJ9R6o7rQtp9M9mlq6NuQIK30GYZEoQJBAN0hh3YYhPmeKLDwGsh/Ys+SbVWsSPkVnu6O9wcMk3JH4bs/n2+hIPvPTYTHixcO/iMwILKdVWSdn5XtKnXR3zMCQQDOPfM7VJbyZVoe0f8iAJfRAZ0d0oq/qTL30ypx3P7VltkcfszQH6M1+gapcZ8mCXpeV510V2U3AlsU+vunS3utAkAE2GM7dzYSsiB6IAi2M/RaT/8NTYUb0Bl3aLKI+QGSE3kivTYlIAa0/cnZCvZFPxLaeod84m2okruYcWXoxvx5AkEAqM5J7FDjL8lHBxzoj2Me38JLYCJ40EDj57Yd8o5owleyosEiUGLkyoQ3ua63DYIKd3eM97GktW6nMDfxjE+bDQJBANcwDRf4CRW646Y/7kizjVOM4MUiJqAoBC4IEawrk25wfo0gBpBbcl24IOTERLUVtMZvqoc6mbNoMUny2LuWvVI=";

		// 加密数据
		String data = "{\"success\":\"success\",\"msg\":\"\\u67e5\\u8be2\\u6210\\u529f\",\"result\":{\"list\":[{\"id\":\"19\",\"no\":\"\\u5180D123654\",\"name\":\"\\u674e\\u5fd7\\u8d85\",\"gps_no\":\"8800003333\",\"gps_type\":\"1\",\"mobile\":\"15631600442\",\"lat\":\"39.32840\",\"lng\":\"115.99546\"},{\"id\":\"169\",\"no\":\"\\u5180D666444\",\"name\":\"\\u674e\\u5fd7\\u8d85\",\"gps_no\":\"8800003248\",\"gps_type\":\"1\",\"mobile\":\"15631600442\",\"lat\":\"40.21509\",\"lng\":\"116.09031\"}]}}";
		System.out.println("------- 加密字符串 -------");
		System.out.println(data);

		// 私钥加签
		String sign = RSAUtils.sign(data, privateKey);
//				String sign = "JAkJLORVds3LC8zv7eaJ3MCWkIzEuwnrlzs7y7zNXYZ8TXRXGJVFBuJ6bhYK5lZMvbHOANCTQSUBQgDUN9Rra38YJW+MG16DxdDvRZlZ8FszySH+5hPuQNGgKkwzdDhabH7/7VhemXWq9VRpkyHxPSnn69I3vhcmba+lGbq2HM4=";
		System.out.println("------- 数据签名 -------");
		System.out.println(sign);

		// 验证签名
		boolean verifySign = RSAUtils.verifySign(data, publicKey, sign);
		System.out.println("------- 验证签名 -------");
		System.out.println(verifySign);

		// 私钥加密
		String priEncrypt = RSAUtils.encryptByPrivateKey(data, privateKey);
		System.out.println("------- 私钥加密 -------");
		System.out.println(priEncrypt);

		// 公钥解密
		String pubDecrypt = RSAUtils.decryptByPublicKey(priEncrypt, publicKey);
		System.out.println("------- 公钥解密 -------");
		System.out.println(pubDecrypt);

		// 公钥加密
		String pubEncrypt = RSAUtils.encryptByPublicKey(data, publicKey);
		System.out.println("------- 公钥加密 -------");
		System.out.println(pubEncrypt);

		// 私钥解密
		String priDecrypt = RSAUtils.decryptByPrivateKey(pubEncrypt, privateKey);
		System.out.println("------- 私钥解密 -------");
		System.out.println(priDecrypt);
	}
	
	
	@Test
	public void test5() throws Exception {
		// 生成新的公私钥
		Map<String, Object> genKeyPair = RSAUtils.resetGenKeyPair();
		// 获取公钥
		String publicKey = RSAUtils.getPublicKey(genKeyPair); // MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzl3e+kQjk+8mxAin+a6cSMPiZUixrSBbMAI1TKOSUYMGoPfeJnMGSsCvvK6oBjZy7y1yq9Ha/6leLXHjnWj3h3Vc0Zbd8MPPN+DXd6+EBTorlPTJXYLdc+4+sgevTXPDYdDzravBuiV4DMJgkRlwRyjccgWoDv0zNmCzJsA2WhkJdg3snFAl0TV1N/IRwX3rCxM/quZa2kjktrlXUoSJ6V53w+0s9UdiTgLrgcBOalH+o88+N27+WLL4yW1h6yLmYG52go5qGyJyLAzndftd3PV/uOPEGUrQevkTQQ5l1q4vYNOgyQ5mDPlRFSJ2sBtM2RzJ0jjSgTBL7Fc2HqATTwIDAQAB   
		// 获取私钥
		String privateKey = RSAUtils.getPrivateKey(genKeyPair);//MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDOXd76RCOT7ybECKf5rpxIw+JlSLGtIFswAjVMo5JRgwag994mcwZKwK+8rqgGNnLvLXKr0dr/qV4tceOdaPeHdVzRlt3ww8834Nd3r4QFOiuU9Mldgt1z7j6yB69Nc8Nh0POtq8G6JXgMwmCRGXBHKNxyBagO/TM2YLMmwDZaGQl2DeycUCXRNXU38hHBfesLEz+q5lraSOS2uVdShInpXnfD7Sz1R2JOAuuBwE5qUf6jzz43bv5YsvjJbWHrIuZgbnaCjmobInIsDOd1+13c9X+448QZStB6+RNBDmXWri9g06DJDmYM+VEVInawG0zZHMnSONKBMEvsVzYeoBNPAgMBAAECggEAf+tQPHHgW+LydLGo7ex+k9xYic/10wsreybjQw5htIUzprP+0Hacf9nRFXRuUuuNcxUI79i1l6S07BacsQZP6XemgYSzLnsyB8EnztjNTTAxs7PI3l4wR5x24IRC9g+8HPYZFp5RB8ISGFak5UyPfIynmUZIVoI9LIfkhTDAgDfHgr6GlZkHY3zHa/BBYCU8aM22m93BEFx4HpQ/+WUE9/nOqkEOL+txbThbiuRYzciL5lAUUFY1syGuSGPU/QNbmD4F9oWN4iaxTlj7fC20+oQt/dtFZk1NMyXvsURmSwqhnye0YSxjx52N/6l4drUKAY6hXE60jmJMRnUO1RxIgQKBgQDx4W22i2KpLux+EaNZ5tejL+nBoXw5gPfcrkouney+fjOF3r/DD5dWZhbuvnTAee4mPoKJ8eB76T6mC/5TloYf06u/1g6PqLH7E4+yavpccNvivkWkk2rjE4plTsYe4uNHI9vtOfkyDHKGqFNqx/xfloXnBi3Q5iQ/9/vfdyQWvQKBgQDaabx8HW7EJG0xxqzOiaTKPOaL/cq98fv7DrVYAhj25N+voFByw4wkwXHubMlDNnTS9U5Z7ZGyrDTk4DSxkHN7qbz6TNe/LNb8DgoALVP6nXKXBHC6/QkK8YuzJN8q7QgHiKFFMrhpNqmKatiS6b/xdAqEJv0WMVZKAkM1HZVo+wKBgG/xr/ArBEdeoy8ZhKtn7ebllt75mN+YM56bv1Nh3Z6wE7JiTAe/4H+pEfX+iVRHWhOzHIExQ361jTwWEuemo355XWSUcfrEzVJxVDDz19kOloWTH/IyE+byNTv7wC+tPBjBnNhm21Dxd9OgdbLWyZOHu4MXVf73dFFyuMxMfJRVAoGAaArkaa9Pm4nKpmKA4SY6ZMhb/qXYFWGXakHYauMc50ehsrTIfdLfCIdJ8YzK1wSy4+dM+pOryxQzYi/XihpImMkDp6xBPeVrR8icRPdSwNiNxJit5rUwwJDNvnm7AWTGZWhVgnQl2qDaZIB/SISnFar0c31rJE4+T6UCROQhz08CgYEAqsq1+JF2ma++Wckjm4Nhnq4XL7frgYvxNnZKEdHSB94df9Or/OCYfqiiSlujDpOq5tzFgYD8bzqk9CszvgeuVkE17dVGDE1POxsRpPL6Go/4SyraGXLNu8kW+cY7nCJwQAdQsgeiUgD3vZ8BeGJfCyVaWpbdT0cbunRrzkvkHho=   
		
		// 公钥加密
		JSONArray jsonArray = JSON.parseArray(jsonStr);
		String ecrpt = RSAUtils.encryptByPublicKey(jsonArray.toString(), publicKey);
		System.out.println(ecrpt);
		
		// 私钥解密
		String decryptInfo = RSAUtils.decryptByPrivateKey(ecrpt, privateKey);
		System.out.println(decryptInfo);
		
	}
	
	
	
	
}
